import random, time
from framework.common.pt_logger import logger


class RandomSleepMiddleware(object):
    def __init__(self, min_sleeping=0, max_sleeping=1):
        self.min_sleeping = min_sleeping
        self.max_sleeping = max_sleeping

    def _set_request(self, request):
        sleeping = random.random() * (self.max_sleeping - self.min_sleeping) + self.min_sleeping  # sleeping 1 to 3 seconds
        logger.info("sleeping random %s seconds..." % sleeping)
        time.sleep(sleeping)
